package cn.micai.base.lambda;


import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * 描述：Java – How to join Arrays
 * <p>
 *     1.Apache Commons Lang – ArrayUtils
       2.Java API
       3.Java 8 Stream
 *
 * @author: 赵新国
 * @date: 2018/6/22 18:14
 */
public class HowToJoinArraysExample {

    public static void main(String [] args) {

        // 1. Apache Commons Lang – ArrayUtils

        /*String [] s1 = new String[] {"a", "b", "c"};
        String [] s2 = new String[] {"d", "e", "f"};

        String [] result = (String[]) ArrayUtils.addAll(s1, s2);
        System.out.println(Arrays.toString(result));

        int [] i1 = new int [] {1,2,3};
        int [] i2 = new int [] {4,5,6};

        int[] result2 = ArrayUtils.addAll(i1, i2);
        System.out.println(Arrays.toString(result2));*/


        // 2. Java API

        /*String [] s1 = new String[] {"a", "b", "c"};
        String [] s2 = new String[] {"d", "e", "f"};
        String [] s3 = new String[] {"g", "h", "j"};

        String [] result = joinArrayGeneric(s1, s2, s3);
        System.out.println(Arrays.toString(result));


        int [] i1 = new int [] {1,2,3};
        int [] i2 = new int [] {4,5,6};
        int [] i3 = new int [] {7,8,9};

        int [] result2 = joinArray(i1, i2, i3);
        System.out.println(Arrays.toString(result2));*/


        // 3. Java 8 Stream

        String[] s1 = new String[]{"a", "b", "c"};
        String[] s2 = new String[]{"d", "e", "f"};
        String[] s3 = new String[]{"g", "h", "i"};

        // join object type array
        String[] result = Stream.of(s1, s2, s3).flatMap(Stream::of).toArray(String[]::new);
        System.out.println(Arrays.toString(result));

        int[] int1 = new int[]{1,2,3};
        int[] int2 = new int[]{4,5,6};
        int[] int3 = new int[]{7,8,9};

        // join 2 primitive type array
        int[] result2 = IntStream.concat(Arrays.stream(int1), Arrays.stream(int2)).toArray();

        // join 3 primitive type array, any better idea?
        int[] result3 = IntStream.concat(Arrays.stream(int1), IntStream.concat(Arrays.stream(int2), Arrays.stream(int3))).toArray();

        System.out.println(Arrays.toString(result2));

        System.out.println(Arrays.toString(result3));


    }

    private static <T> T[] joinArrayGeneric(T[] ... arrays) {
        int length = 0;
        for (T[] array : arrays) {
            length += array.length;
        }

        // T[] result = new T[length];
        final T [] result = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), length);

        int offset = 0;
        for (T[] array : arrays) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }

        return result;

    }

    private static int [] joinArray(int [] ... arrays) {
        int length = 0;
        for (int [] array : arrays) {
            length += array.length;
        }

        // T[] result = new T[length];
        final int [] result = new int[length];

        int offset = 0;
        for (int [] array : arrays) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }

        return result;
    }
}
